package com.hygj.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hygj.pojo.Intermediary;
import com.hygj.service.IntermediaryService;
import com.hygj.util.PageUtil;

@Controller
@RequestMapping
public class IntermediaryController {
	
	@Autowired
	private IntermediaryService intermediaryService;
	
	/**
	 * 保存
	 * 
	 * @param intermediary
	 * @return
	 */
	@RequestMapping(value = "insertIntermediary", method = RequestMethod.POST)
	public String insertIntermediary(Map<String, Object> map, Intermediary intermediary) {
		intermediaryService.insert("insert", intermediary);
		map.put("intermediary", intermediary);
		return "intermediarys/intermediaryPrint";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteIntermediary")
	public String deleteIntermediary(@RequestParam("id") Integer id) {
		intermediaryService.delete("delete", id);

		return "redirect:getIntermediaryAll";
	}
    
	/**
	 * 查询全部
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getIntermediaryAll")
	public String getIntermediaryAll(@ModelAttribute("search") Intermediary intermediary, Map<String, Object> map,
			HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 判断长度，是否为空
		if (StringUtils.isNotBlank(intermediary.getInter_missionName())) {
			params.put("inter_missionName", "%" + intermediary.getInter_missionName() + "%");

		}
		PageUtil<Intermediary> page = intermediaryService.page(request, 10, params);
		map.put("page", page);
		return "intermediarys/intermediaryShow";
	}

	/**
	 * 详情页面
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getIntermediaryById")
	public String getIntermediaryById(@RequestParam("id") Integer id, ModelMap modelMap) {
		Intermediary intermediarys = intermediaryService.getById("getById", id);
		modelMap.addAttribute("intermediarys", intermediarys);
		return "intermediarys/intermediaryDetails";
	}

	/**
	 * 打印页面
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getIntermediaryByIds")
	public String getIntermediaryByIds(@RequestParam("id") Integer id, ModelMap modelMap) {
		Intermediary intermediary = intermediaryService.getById("getById", id);
		modelMap.addAttribute("intermediary", intermediary);
		return "intermediarys/intermediaryPrintOne";
	}

}
